# Maintainer: Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Kristian Mosegaard <kristian@mosen.me>
# Contributor: Max Liebkies <mail@maxliebkies.de>
# Contributor: Krzysztof Bogacki <krzysztof.bogacki@leancode.pl>

pkgbase=dotnet-core-3.1
pkgname=(
 dotnet-runtime-3.1
 aspnet-runtime-3.1
 dotnet-sdk-3.1
 dotnet-targeting-pack-3.1
 aspnet-targeting-pack-3.1
)
pkgver=3.1.20.sdk120
pkgrel=1
arch=(loong64 x86_64)
url=https://www.microsoft.com/net/core
license=(MIT)
makedepends=(
  bash
  clang
  cmake
  curl
  #dotnet-sdk-3.1
  git
  icu
  inetutils
  krb5
  libgit2
  libunwind
  libxml2
  lldb
  llvm
  lttng-ust2.12
  nodejs
  openssl
  systemd
  zlib
)
options=(staticlibs)
_tag=61c1106e21baad31a946238a4aa2b27fde415c97
source=(
  dotnet-source-build::git+https://github.com/dotnet/source-build.git#tag=${_tag}
  dotnet-source-build-coreclr-tools-path.patch
  9999-corefx-add-arch-rids.patch
  9999-corefx-fix-build-clang10.patch
  9999-core-setup-fix-computed-rid.patch
)
b2sums=('SKIP'
        'bd6bba84f851ce25b8dda085af1415acc14e50c6c99712dc03d51cdb3dbb362e7dc0ee16d54b27a1d46eb0db03be5dd0c1dae6c2a8a9ab0e46b2bf6c76292989'
        '2e56d6458c50c95e5d2489235b13727c8d708162318cad1cc05b225d47d736dbd9bc897eb3eb7014f716423dcfef67178efbdee41be30e64bac120912efeabd4'
        '831a76ec9c12498edcb0e8085f3f725e8ebecc951c8ae9cbd871330ffb813cce42e03e28c0fe1493cdd629936feff98ae26f48f50435039ac38fbba53a9371ac'
        'bd9e0cec11937997df2764bccfdb9468af8ffabd85597447a3de52c801cd6c2717fb297f4bad00784568c67b4f1648038cc0ffdccc7f52c63935562bc2f2ef9a')

prepare() {
  #cp -r /usr/share/dotnet .

  cd dotnet-source-build

  patch -Np1 -i ../dotnet-source-build-coreclr-tools-path.patch

  [ -d patches/corefx ] || mkdir patches/corefx
  cp ../9999-corefx-add-arch-rids.patch patches/corefx/
  cp ../9999-corefx-fix-build-clang10.patch patches/corefx/
  [ -d patches/core-setup ] || mkdir patches/core-setup
  cp ../9999-core-setup-fix-computed-rid.patch patches/core-setup/
}

pkgver() {
  cd dotnet-source-build

  if [[ $(git describe --tags) != v3.1.*-runtime && $(git describe --tags) != v3.1.*-SDK ]]; then
    exit 1
  fi

  local _runtimever=$(xmllint --xpath "//Dependency[@Name='Microsoft.NETCore.App.Runtime.linux-x64']/@Version" eng/Version.Details.xml | cut -d '=' -f 2 | sed 's/^"//; s/"$//')
  local _sdkver=$(xmllint --xpath "//Dependency[@Name='Microsoft.NET.Sdk']/@Version" eng/Version.Details.xml | cut -d '=' -f 2 | sed 's/^"//; s/"$//; s/-rtm.*//; s/-servicing.*//')

  echo "${_runtimever}.sdk${_sdkver##*.}"
}

build() {
  cd dotnet-source-build

  export SOURCE_BUILD_SKIP_SUBMODULE_CHECK=1

  ./build.sh \
    /p:ArchiveDownloadedPackages=true \
    /p:ContinueOnPrebuiltBaselineError=true \
    /p:SkipPortableRuntimeBuild=true \
    /p:SkipPrebuiltEnforcement=true \
    /p:UseSystemLibraries=true \
    /p:UseSystemLibunwind=true
    #--with-sdk "${srcdir}"/dotnet \
}

package_dotnet-runtime-3.1() {
  pkgdesc='The .NET Core runtime'
  depends=(
    dotnet-host
    gcc-libs
    glibc
    icu
    libcurl.so
    libgssapi_krb5.so
    libunwind
    openssl
    zlib
  )

  optdepends=('lttng-ust2.12: CoreCLR tracing')

  cd dotnet-source-build/artifacts/x64/Release

  install -dm 755 "${pkgdir}"/usr/share/{dotnet,licenses}
  bsdtar -xf dotnet-sdk-${pkgver%.*.sdk*}.${pkgver#*sdk}-arch-x64.tar.gz -C "${pkgdir}"/usr/share/dotnet/ --no-same-owner shared/Microsoft.NETCore.App
  ln -s dotnet-host "${pkgdir}"/usr/share/licenses/dotnet-runtime-3.1
}

package_aspnet-runtime-3.1() {
  pkgdesc='The ASP.NET Core runtime'
  depends=(dotnet-runtime-3.1)

  cd dotnet-source-build/artifacts/x64/Release

  install -dm 755 "${pkgdir}"/usr/share/{dotnet,licenses}
  bsdtar -xf dotnet-sdk-${pkgver%.*.sdk*}.${pkgver#*sdk}-arch-x64.tar.gz -C "${pkgdir}"/usr/share/dotnet/ --no-same-owner shared/Microsoft.AspNetCore.App
  ln -s dotnet-host "${pkgdir}"/usr/share/licenses/aspnet-runtime-3.1
}

package_dotnet-sdk-3.1() {
  pkgdesc='The .NET Core SDK'
  depends=(
    dotnet-runtime-3.1
    dotnet-targeting-pack-3.1
    glibc
    gcc-libs
    netstandard-targeting-pack
  )
  optdepends=('aspnet-targeting-pack-3.1: Build ASP.NET Core applications')

  cd dotnet-source-build/artifacts/x64/Release

  install -dm 755 "${pkgdir}"/usr/share/{dotnet,licenses}
  bsdtar -xf dotnet-sdk-${pkgver%.*.sdk*}.${pkgver#*sdk}-arch-x64.tar.gz -C "${pkgdir}"/usr/share/dotnet/ --no-same-owner sdk templates
  ln -s dotnet-host "${pkgdir}"/usr/share/licenses/dotnet-sdk-3.1
}

package_dotnet-targeting-pack-3.1() {
  pkgdesc='The .NET Core targeting pack'
  depends=(netstandard-targeting-pack)

  cd dotnet-source-build/artifacts/x64/Release

  install -dm 755 "${pkgdir}"/usr/share/{dotnet,licenses}
  bsdtar -xf dotnet-sdk-${pkgver%.*.sdk*}.${pkgver#*sdk}-arch-x64.tar.gz -C "${pkgdir}"/usr/share/dotnet/ --no-same-owner packs/Microsoft.NETCore.App.{Host.arch-x64,Ref}
  ln -s dotnet-host "${pkgdir}"/usr/share/licenses/dotnet-targeting-pack-3.1
}

package_aspnet-targeting-pack-3.1() {
  pkgdesc='The ASP.NET Core targeting pack'
  depends=(dotnet-targeting-pack-3.1)

  cd dotnet-source-build/artifacts/x64/Release

  install -dm 755 "${pkgdir}"/usr/share/{dotnet,licenses}
  bsdtar -xf dotnet-sdk-${pkgver%.*.sdk*}.${pkgver#*sdk}-arch-x64.tar.gz -C "${pkgdir}"/usr/share/dotnet/ --no-same-owner packs/Microsoft.AspNetCore.App.Ref
  ln -s dotnet-host "${pkgdir}"/usr/share/licenses/aspnet-targeting-pack-3.1
}

# vim: ts=2 sw=2 et:
